<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>设置脚本中所有日期/时间函数使用的默认时区</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.date-default-timezone-get.html">« date_default_timezone_get</a></li>
      <li style="float: right;"><a href="function.date-diff.html">date_diff »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.datetime.html">Date/Time 函数</a></li>
    <li>设置脚本中所有日期/时间函数使用的默认时区</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.date-default-timezone-set" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">date_default_timezone_set</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">date_default_timezone_set</span> &mdash; <span class="dc-title">
   设置脚本中所有日期/时间函数使用的默认时区
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.date-default-timezone-set-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>date_default_timezone_set</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$timezoneId</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>date_default_timezone_set()</strong></span> 设置所有日期/时间函数使用的默认时区
  </p>
  <p class="para">
   除了在脚本中使用此函数设置默认时区，还可以使用 INI 设置 <a href="datetime.configuration.html#ini.date.timezone" class="link">date.timezone</a>
   设置默认时区。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.date-default-timezone-set-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">timezoneId</code></dt>

     <dd>

      <p class="para">
       时区标识符，像 <code class="literal">UTC</code>、<code class="literal">Africa/Lagos</code>、<code class="literal">Asia/Hong_Kong</code>
       或 <code class="literal">Europe/Lisbon</code>。有效的标识符列表见<a href="timezones.html" class="xref">所支持的时区列表</a>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.date-default-timezone-set-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">timezoneId</code> 无效，此函数返回 <strong><code>false</code></strong>，否则返回 <strong><code>true</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.date-default-timezone-set-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2072">
    <p><strong>示例 #1 获取默认时区</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />date_default_timezone_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'America/Los_Angeles'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$script_tz&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_default_timezone_get</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">strcmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$script_tz</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'date.timezone'</span><span style="color: #007700">))){<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Script&nbsp;timezone&nbsp;differs&nbsp;from&nbsp;ini-set&nbsp;timezone.'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Script&nbsp;timezone&nbsp;and&nbsp;ini-set&nbsp;timezone&nbsp;match.'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.date-default-timezone-set-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.date-default-timezone-get.html" class="function" rel="rdfs-seeAlso">date_default_timezone_get()</a> - 取得脚本中所有日期/时间函数所使用的默认时区</span></li>
    <li class="member"><a href="timezones.html" class="xref">所支持的时区列表</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>